package com.hangwei.paperless.view.repair;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hangwei.paperless.MainRepairActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.adapter.SubProcessAdapter;
import com.hangwei.paperless.model.bean.CardToSeal;
import com.hangwei.paperless.model.bean.RecordListBean;
import com.hangwei.paperless.model.bean.SubProcessInfo;
import com.hangwei.paperless.model.bean.SubRepairList;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.presenter.SubProcessPresenter;
import com.hangwei.paperless.utils.JsonPostUtil;
import com.hangwei.paperless.utils.NFCCallBack;
import com.hangwei.paperless.view.BaseView;
import com.hangwei.paperless.view.SealHelper;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张龙臣 on 2020/10/14.
 */

public class SubProcess extends BaseView<MainRepairActivity, SubProcessInfo> {

    private EditText mEtRemark;
    private SubProcessAdapter mAdapter;
    private RelativeLayout mFlUserSeal;
    private SubProcessPresenter mPresenter;

    public SubProcess(MainRepairActivity activity) {
        super(activity);
    }

    @Override
    protected View initView() {
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.page_sub_process, null);
//        ImageView ivInspectorSeal = (ImageView) inflate.findViewById(R.id.ivInspectorSeal);
        TextView tvEffective = (TextView) inflate.findViewById(R.id.tvEffective);
        SwipeRefreshLayout refreshLayout = inflate.findViewById(R.id.refreshLayout);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        tvEffective.setOnClickListener(v -> {
            List<RecordListBean> data = mAdapter.getData();
            ArrayList<String> list = new ArrayList<>();
            for (RecordListBean bean : data) {
                if (bean.isChecked)
                    list.add(bean.id);
            }
            if (list.size() == 0) {
                ToastUtil.showCenterToast("请选择工卡！");
            } else {
                LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                loadingDialog.show();
                String userId = SPUtil.getString(SPKey.Account.USER_ID);
                StaticPresenter.effective(userId, mActivity.getBillId(), list, new CallBack<Object>() {
                    @Override
                    public void onCompleted(Object o) {
                        loadingDialog.dismiss();
                        if (mPresenter!=null)
                            mPresenter.refresh();
                    }

                    @Override
                    public void onError() {
                        loadingDialog.dismiss();
                    }
                });
            }
        });

        mAdapter = new SubProcessAdapter(null) {
            @Override
            protected void savePn(String id, String pn, final CallBack<Object> callBack) {
                final LoadingDialog dialog = new LoadingDialog(mActivity);
                dialog.show();
                String userId = SPUtil.getString(SPKey.Account.USER_ID);
                String jsonData = JsonPostUtil.buildSubProcessSave(userId, mActivity.getBillId(), id, pn);
//                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData);
                StaticPresenter.saveRoutingSheetListInfo(userId, mActivity.getBillId(), id, pn, new CallBack<Object>() {
                    @Override
                    public void onCompleted(Object o) {
                        dialog.dismiss();
                        callBack.onCompleted(null);
                    }

                    @Override
                    public void onError() {
                        dialog.dismiss();
                        callBack.onError();
                    }
                });
            }
        };

        mPresenter = new SubProcessPresenter(refreshLayout, mAdapter, recyclerView);
        mPresenter.setBillId(mActivity.getBillId());
        recyclerView.setAdapter(mAdapter);
        mAdapter.addFooterView(getFooter());
        mAdapter.setEnableLoadMore(false);
        LoadingDialog loadingDialog = new LoadingDialog(mActivity);
        loadingDialog.show();
        mPresenter.refresh(new CallBack<SubRepairList>() {
            @Override
            public void onCompleted(SubRepairList subRepairList) {
                loadingDialog.dismiss();
                mAdapter.setEnableLoadMore(true);
                if (subRepairList != null) {
                    mEtRemark.setText(Tools.checkedString(subRepairList.remark));
                    SealHelper sealHelper = new SealHelper(mActivity) {
                        @Override
                        protected void onSeal() {
                            SubProcess.this.mActivity.sealByCard("", new NFCCallBack() {
                                @Override
                                public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                                    if (stampIds == null || stampIds.size() == 0) {
                                        ToastUtil.showCenterToast("无印章！");
                                    } else {
                                        final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                                        loadingDialog.show();
                                        String userId0 = SPUtil.getString(SPKey.Account.USER_ID);
                                        String remark = mEtRemark.getText().toString().trim();
                                        StaticPresenter.stampRoutingSheetContent(userId0, SubProcess.this.mActivity.getBillId(), 5, "2", userId, stampIds.get(0).stampId, remark, new CallBack<Object>() {
                                            @Override
                                            public void onCompleted(Object o) {
                                                loadingDialog.dismiss();
                                                SubProcess.this.mActivity.updateProgress();
                                            }

                                            @Override
                                            public void onError() {
                                                loadingDialog.dismiss();
                                            }
                                        });
                                    }
                                }
                            });
                        }
                    };
                    sealHelper.bindData(new SealHelper.SealBean(true, subRepairList.inspectorStampId, subRepairList.inspectorId, subRepairList.inspectorStampTypeCode, subRepairList.inspectorStampCode, subRepairList.inspectorDate, subRepairList.inspector, true));
                    mFlUserSeal.removeAllViews();
                    mFlUserSeal.addView(sealHelper.getItemView());
                }
            }

            @Override
            public void onError() {
                loadingDialog.dismiss();
            }
        });
//        initData();
        return inflate;
    }

    private View getFooter() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.footer_sub_process, null);
        mEtRemark = (EditText) view.findViewById(R.id.etRemark);
        mEtRemark.setEnabled(false);
        mFlUserSeal = view.findViewById(R.id.flUserSeal);
        return view;
    }

    @Override
    public void bindData(SubProcessInfo subProcessInfo) {
        mEtRemark.setText(Tools.checkedString(subProcessInfo.remark));
        SealHelper sealHelper = new SealHelper(mActivity) {
            @Override
            protected void onSeal() {
                SubProcess.this.mActivity.sealByCard("", new NFCCallBack() {
                    @Override
                    public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                        if (stampIds == null || stampIds.size() == 0) {
                            ToastUtil.showCenterToast("无印章！");
                        } else {
                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                            loadingDialog.show();
                            String userId0 = SPUtil.getString(SPKey.Account.USER_ID);
                            String remark = mEtRemark.getText().toString().trim();
                            StaticPresenter.stampRoutingSheetContent(userId0, SubProcess.this.mActivity.getBillId(), 5, "2", userId, stampIds.get(0).stampId, remark, new CallBack<Object>() {
                                @Override
                                public void onCompleted(Object o) {
                                    loadingDialog.dismiss();
                                    SubProcess.this.mActivity.updateProgress();
                                }

                                @Override
                                public void onError() {
                                    loadingDialog.dismiss();
                                }
                            });
                        }
                    }
                });
            }
        };
        sealHelper.bindData(new SealHelper.SealBean(true, subProcessInfo.inspectorStampId, subProcessInfo.inspectorId, subProcessInfo.inspectorStampTypeCode, subProcessInfo.inspectorStampCode, subProcessInfo.inspectorDate, subProcessInfo.inspector, true));
        mFlUserSeal.removeAllViews();
        mFlUserSeal.addView(sealHelper.getItemView());
        mAdapter.setNewData(subProcessInfo.recordList);
    }

    private void initData() {
        final LoadingDialog dialog = new LoadingDialog(mActivity);
        dialog.show();
        String userId = SPUtil.getString(SPKey.Account.USER_ID);
        StaticPresenter.getRoutingSheetList(userId, mActivity.getBillId(), new CallBack<SubProcessInfo>() {
            @Override
            public void onCompleted(SubProcessInfo subProcessInfo) {
                bindData(subProcessInfo);
                dialog.dismiss();
            }

            @Override
            public void onError() {
                dialog.dismiss();
            }
        });
    }

//    @Override
//    protected void isNotFastClick(View v) {
//        switch (v.getId()) {
//            case R.id.ivInspectorSeal:
//                mActivity.sealByCard("", new SealCallBack() {
//                    @Override
//                    public void onCallBack(List<CardToSeal.StampBean> stampIds) {
//                        if (stampIds == null || stampIds.size() == 0) {
//                            ToastUtil.showCenterToast("无印章！");
//                        } else {
//                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
//                            loadingDialog.show();
//                            String userId = SPUtil.getString(SPKey.Account.USER_ID);
//                            String remark = mEtRemark.getText().toString().trim();
//                            StaticPresenter.stampRoutingSheetContent(userId, mActivity.getBillId(), 5, "2", userId, stampIds.get(0).stampId, remark, new CallBack<Object>() {
//                                @Override
//                                public void onCompleted(Object o) {
//                                    loadingDialog.dismiss();
//                                    mActivity.updateProgress();
//                                }
//
//                                @Override
//                                public void onError() {
//                                    loadingDialog.dismiss();
//                                }
//                            });
//                        }
//                    }
//                });
//
////                SealUtil.onSeal(new MainBottomPopupWindow.OnSealListener() {
////                    @Override
////                    public void onSeal(String stampId) {
////                        mActivity.closeBottomPop();
////                        final LoadingDialog dialog = new LoadingDialog(mActivity);
////                        dialog.show();
////                        String userId = SPUtil.getString(SPKey.Account.USER_ID);
////                        String remark = mEtRemark.getText().toString().trim();
////                        StaticPresenter.stampRoutingSheetContent(userId, mActivity.getBillId(), 5, "2", userId, stampId, remark, new CallBack<Object>() {
////                            @Override
////                            public void onCompleted(Object o) {
////                                dialog.dismiss();
////                                mActivity.updateProgress();
////                            }
////
////                            @Override
////                            public void onError() {
////                                dialog.dismiss();
////                            }
////                        });
////                    }
////                }, mActivity);
//                break;
//        }
//    }
}
